// 这个store存储大家公共使用的数据与方法. 不局限在system中的数据. 从system改名而来.
import { Module } from 'vuex';
import { IRootState } from '../../types';
import { ICommonState } from './common-types';

import {
  requestPageListData,
  deleteDataInPageList,
  createDataInPageList,
  editDataInPageList
} from '@/service/main/system/system';

const commonModule: Module<ICommonState, IRootState> = {
  namespaced: true,
  state() {
    return {
      usersList: [],
      usersCount: 0,
      roleList: [],
      roleCount: 0,
      goodsList: [],
      goodsCount: 0,
      menuList: [],
      menuCount: 0
    };
  },
  getters: {
    pageList(state) {
      return (pageName: string) => {
        return (state as any)[`${pageName}List`];
        /* switch (pageName) {
          case 'users':
            return state.usersList;
          case 'role':
            return state.roleList;
        } */
      };
    },
    pageListCount(state) {
      return (pageName: string) => {
        return (state as any)[`${pageName}Count`];
      };
    }
  },
  mutations: {
    changeUsersList(state, userList: any[]) {
      state.usersList = userList;
    },
    changeUsersCount(state, totalCount: number) {
      state.usersCount = totalCount;
    },
    changeRoleList(state, roleList: any[]) {
      state.roleList = roleList;
    },
    changeRoleCount(state, roleCount: number) {
      state.roleCount = roleCount;
    },
    changeGoodsList(state, goodsList: any[]) {
      state.goodsList = goodsList;
    },
    changeGoodsCount(state, goodsCount: number) {
      state.goodsCount = goodsCount;
    },
    changeMenuList(state, menuList: any[]) {
      state.menuList = menuList;
    },
    changeMenuCount(state, menuCount: number) {
      state.menuCount = menuCount;
    }
  },
  actions: {
    async getPageListAction({ commit }, payload) {
      console.log(payload);

      // 1.发送请求获取user需要展示的数据
      const pageResult = await requestPageListData(
        payload.pageUrl,
        payload.queryInfo
      );
      console.log(pageResult);

      // 2.更新store的数据
      // 首字母转为大写
      let pageName = payload.pageName;
      pageName = pageName.replace(pageName[0], pageName[0].toUpperCase());
      const { list, totalCount } = pageResult.data;
      commit('change' + pageName + 'List', list);
      commit('change' + pageName + 'Count', totalCount);
    },

    async deletePageListAction(context, payload) {
      // 1.开始删除
      // 告诉我删除哪个页面的数据(pageName),id是多少,我拼接好了在告诉发送请求的人
      const pageName = payload[0];
      const id = payload[1];
      const url = `/${pageName}/${id}`;
      await deleteDataInPageList(url);
      // 2.更新列表 直接在page-content里面做了,里面可以直接调用getPageListAction,且有现成的参数
      // dispatch('commonModule/getPageListAction', )
    },

    async createPageListAction(context, payload) {
      console.log('createPageListAction', payload);
      await createDataInPageList(payload.url, payload.formInfo);
    },

    async editPageListAction(context, payload) {
      await editDataInPageList(payload.url, payload.formInfo);
    }
  }
};

export default commonModule;
